;; signal.scm - (c) rohan drape, 2005

;; A Signal is half the size of a Wavetable, each element is the sum
;; of two adjacent elements of the Wavetable.

(define (wavetableV->signalV w)
  (let* ((size (vector-length w))
	 (size2 (/ size 2))
	 (s (make-vector size2)))
    (for-each (lambda (i) 
		(let* ((j (* i 2)) (k (+ j 1)))
		  (vector-set! s i (+ (vector-ref w j) (vector-ref w k)))))
	      (iota size2))
    s))

;; A Wavetable is twice the size of a Signal.  Each element 'e0'
;; expands to {2*e0-e1, e1-e0} where e1 is the next element.

(define (signalV->wavetableV s)
  (let* ((size (vector-length s))
	 (w (make-vector (* size 2))))
    (vector-for-each (lambda (i n)
		       (let* ((j (* i 2)) (k (+ j 1)))
			 (let ((v1 (vector-ref s i)) 
			       (v2 (vector-ref s (if (= i (- size 1)) 0 (+ i 1)))))
			   (vector-set! w j (- (* 2.0 v1) v2))
			   (vector-set! w k (- v2 v1)))))
		     s)
    w))

(define (signalL->wavetableL s) (vector->list (signalV->wavetableV (list->vector s))))
(define (wavetableL->signalL w) (vector->list (wavetableV->signalV (list->vector w))))
